package com.imooc.miaosha.config;

import com.imooc.miaosha.redis.keys.GoodsKey;
import com.imooc.miaosha.redis.RedisFactory;
import com.imooc.miaosha.service.GoodsService;
import com.imooc.miaosha.vo.GoodsVO;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模块描述: <br>
 * (秒杀初始化)
 *
 * @Author: Mr. xyb
 * @Date: 2019/4/24 16:17
 * @since: 1.8.0
 * @version: 1.0.0
 */
@Component
public class SeckillInitialization implements InitializingBean {
    public Map<Long, Boolean> localOver = new HashMap<>();

    /**
     * 将商品库存同步到redis中
     *
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        List<GoodsVO> goodsVOS = goodsService.listGoodsVo();
        if (goodsVOS == null && goodsVOS.size() == 0) {
            return;
        }
        for (GoodsVO good : goodsVOS) {
            redisFactory.set(GoodsKey.seckillGoodsStock, "" + good.getId(), good.getStockCount());
            localOver.put(good.getId(), false);
        }
    }

    @Autowired
    private RedisFactory redisFactory;

    @Autowired
    private GoodsService goodsService;
}
